package top.appx.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.appx.entity.FoodEntity;
import top.appx.entity.OrderEntity;
import top.appx.qo.FoodQO;
import top.appx.service.FoodService;
import top.appx.service.OrderService;
import top.appx.util.StringUtil;
import top.appx.vo.FoodVO;
import top.appx.vo.Pager;
import top.appx.vo.ResultVO;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * Created by john on 2015/7/12.
 */
@Controller
public class FoodController {

    @Resource
    private FoodService foodService;

    @Resource
    private OrderService orderService;
    @RequestMapping(value="/prj/food")
    public String home(FoodQO foodQO,HttpServletRequest request) throws UnsupportedEncodingException {

        Pager<FoodVO> pager = foodService.queryByPage(foodQO);
        request.setAttribute("pager",pager);
        request.setAttribute("foodQO",foodQO);

        String ip = request.getRemoteAddr();

        String oldRname = "";
        String oldEmail = "";
        Cookie[] cookies = request.getCookies();
        if(cookies!=null){
            for (Cookie cookie :cookies){
                if("oldRname".equals(cookie.getName())){
                    if(cookie.getValue()!=null) {
                        oldRname = URLDecoder.decode(cookie.getValue(), "utf-8");
                    }
                }
                else if("oldEmail".equals(cookie.getName())){
                    if(cookie.getValue()!=null) {
                        oldEmail = URLDecoder.decode(cookie.getValue(), "utf-8");
                    }
                }
            }
        }

        request.setAttribute("oldRname",oldRname);
        request.setAttribute("oldEmail",oldEmail);

     /*   OrderEntity oe = orderService.queryByIp(ip);
        if(oe!=null){
            request.setAttribute("oldRname",oe.getRname());
            request.setAttribute("oldEmail",oe.getEmail());
        }*/

        return "food";
    }
    @ResponseBody
    @RequestMapping(value="food/controller",params = "action=queryByPage")
    public Object queryByPage(FoodQO foodQO){
        Pager<FoodVO> pager =  foodService.queryByPage(foodQO);
        return pager;
    }


    @ResponseBody
    @RequestMapping(value="food/controller",params = "action=save")
    public Object save(FoodEntity food){
        foodService.save(food);
        return ResultVO.success();
    }
    @RequestMapping(value="food/controller",params = "action=delById")
    @ResponseBody
    public Object delById(int id){
        foodService.delById(id);
        return ResultVO.success();
    }
    @RequestMapping(value="food/controller",params = "action=modify")
    @ResponseBody
    public Object modify(FoodEntity food){
        foodService.modify(food);
        return ResultVO.success();
    }


}
